<!-- 我的设备 -->

<template>
  <div>
    <FormSlot label="播出设备" bind="deviceList" tipPre="请选择" required>
      <BtnPrimary title="选择设备" :disabled="isShow" @click="toOpen" />
    </FormSlot>

    <WebTable :list="one.deviceList" noPage>
      <RowIndex />
      <RowText label="设备编号" bind="deviceCode" />
      <RowTitle label="设备名称" bind="deviceName" :minWidth="160" />
      <RowTree :label="`所在${CORE.MAP.REGION}`" bind="areaId" :dict="CORE.REGION.DICT" allName :minWidth="120" />
      <RowOperate :btns="['del']" :onDel="toDel" :width="80" />
    </WebTable>

    <EasyModal v-model="easyVisible" title="选择设备" large @submit="toSubmit" @close="toReset">
      <ChooseIOT :obj="obj" bind="deviceList" deviceCategoryId="23" disableCategory />
    </EasyModal>
  </div>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'MyDevice',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb()],

  /**
   * 状态数据 (函数返回对象)
   */
  data() {
    return {
      easyVisible: false, // 弹窗是否可见
      obj: { deviceList: [] }, // 数据对象
    }
  },

  /**
   * 监听属性 (深度 deep 首次 immediate)
   */
  watch: {
    /**
     * 监听弹窗可见
     */
    easyVisible(visible) {
      visible && this.toReset()
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 弹窗
     */
    toOpen() {
      this.easyVisible = true
    },

    /**
     * 提交
     */
    toSubmit() {
      this.easyVisible = false
      this.one.deviceList = CORE.clone(this.obj.deviceList)
      this.one.snList = this.one.deviceList.map((item) => item.thirdCode)
    },

    /**
     * 重置
     */
    toReset() {
      this.obj = { deviceList: CORE.clone(this.one.deviceList) }
    },

    /**
     * 清除
     */
    toClear() {
      this.obj = { deviceList: [] }
      this.one.deviceList = this.one.snList = []
    },

    /**
     * 删除
     */
    toDel(row) {
      const deviceList = this.one.deviceList || []
      const index = deviceList.findIndex((item) => item.id === row.id)
      index >= 0 && deviceList.splice(index, 1)
    },
  },
}
</script>
